package madixin.t38;

class Solution {
    public String countAndSay(int n) {
        if (n < 1) {
            return "";
        }
        StringBuilder res = new StringBuilder();
        res.append(1);
        for (int i = 1; i < n; i++) {
            String cur = res.toString();
            res = new StringBuilder();
            for (int j = 0; j < cur.length(); j++) {
                int count = 1;
                while (j < cur.length() - 1 && cur.charAt(j) == cur.charAt(j + 1)) {
                    count++;
                    j++;
                }
                res.append(count).append(cur.charAt(j));
            }
        }
        return res.toString();
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        //System.out.println(s.countAndSay(1));
        System.out.println(s.countAndSay(2));
        System.out.println(s.countAndSay(3));
        System.out.println(s.countAndSay(4));
        System.out.println(s.countAndSay(5));
        System.out.println(s.countAndSay(6));
    }
}